﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            DateTime startTime = new DateTime(2021, 1, 1, 0, 0, 0); // 开始时间
            DateTime endTime = new DateTime(2021, 1, 2, 0, 0, 0); // 结束时间
            TimeSpan interval = TimeSpan.FromMinutes(5); // 时间间隔

            Random random = new Random(); // 随机数生成器
            List<(DateTime Timestamp, double Value)> data = new List<(DateTime, double)>(); // 时间和值的集合

            for (DateTime time = startTime; time < endTime; time += interval)
            {
                double value = random.NextDouble() * 100; // 在 0 到 100 之间生成一个随机数
                data.Add((time, value)); // 添加时间和值到集合中
            }

            var result = data.GroupBy(d => d.Timestamp - startTime - TimeSpan.FromTicks((d.Timestamp - startTime).Ticks % interval.Ticks))
                .Select(g => new
                {
                    Timestamp = startTime.Add(g.Key),
                    Value = g.Average(d => d.Value)
                })
                .ToList();

            foreach (var item in result)
            {
                Console.WriteLine($"时间：{item.Timestamp},值：{item.Value}");
            }

            Console.ReadLine();
        }
    }
}
